<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { getLocalIcons } from '@/utils/icon';

defineOptions({ name: 'IconPage' });

// 本地图标列表
const localIcons = ref([]);
// 表格加载状态
const loading = ref(false);

// 初始化
onMounted(() => {
  loadIcons();
});

// 加载图标列表
function loadIcons() {
  loading.value = true;
  try {
    // 仅获取local-icon目录下的图标
    const icons = getLocalIcons();
    localIcons.value = icons.map((name, index) => ({
      id: index + 1,
      name
    }));
    console.log('加载的图标:', localIcons.value);
  } catch (error) {
    console.error('加载图标失败:', error);
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <div class="h-full">
    <el-card class="mt-10px">
      <template #header>
        <div class="flex justify-between items-center">
          <span>本地图标管理</span>
        </div>
      </template>
      
      <el-table :data="localIcons" border v-loading="loading" row-key="id">
        <el-table-column label="ID" prop="id" width="80" sortable />
        <el-table-column label="图标名称" prop="name" min-width="120" />
        <el-table-column label="图标预览" width="120">
          <template #default="{ row }">
            <div class="flex justify-center">
              <svg-icon :local-icon="row.name" class="text-30px text-primary" />
            </div>
          </template>
        </el-table-column>
      </el-table>
      
      <ElCard header="Icon组件示例" class="card-wrapper">
      <div class="grid grid-cols-10">
        <template v-for="item in icons" :key="item">
          <div class="mt-5px flex-x-center">
            <SvgIcon :icon="item" class="text-30px" />
          </div>
        </template>
      </div>
      <div class="mt-50px">
        <h1 class="mb-20px text-18px font-500">Icon图标选择器</h1>
        <CustomIconSelect v-model:value="selectValue" :icons="icons" />
      </div>
      <template #footer>
        <WebSiteLink label="iconify地址：" link="https://icones.js.org/" class="mt-10px" />
      </template>
    </ElCard>
    </el-card>
  </div>
</template>
